<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\property\basic;

use app\common\model\BaseModel;

class PropertyUnitModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'property_unit';
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    public function getFullUnitNameAttr()
    {

        $roomUnitInfo = self::getDB()->where('id', $this->id)->find();
        if ($roomUnitInfo) {
            $pids = array_filter(explode('/', $roomUnitInfo['path']));
            $pids[] = $roomUnitInfo['id'];
            $units = self::getDB()->where('id', 'in', $pids)->column('unit_name');
            return implode(' - ', $units);
        }
        return '';
    }
}
